package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.ArticleType;
import com.czh.springboot04.service.ArticleTypeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("type")
//@CrossOrigin
@Api("文章分类接口")
public class ArticleTypeController {
    @Autowired
    ArticleTypeService articleTypeService;
    @ApiOperation("添加文章分类")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name",value = "文章分类名称",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 1,message = "成功"),
            @ApiResponse(code = 0,message = "失败"),
            @ApiResponse(code = -1,message = "服务器忙")
    })
    @PostMapping("addType")
    public R addType(@RequestBody ArticleType articleType){
        int i = articleTypeService.addArticleType(articleType);
        if(i > 0){
            return R.ok(articleType);
        }else {
            return R.fail("失败");
        }
    }

    @PutMapping("updateType")
    public R updateType(@RequestBody ArticleType articleType){
        int i = articleTypeService.updateArticleType(articleType);
        if(i > 0){
            return R.ok("成功");
        }else {
            return R.fail("失败");
        }
    }


    @DeleteMapping("/{id}")
    public R deleteType(@PathVariable("id")  int id){
        int i = articleTypeService.deleteArticleType(id);
        if(i > 0){
            return R.ok("成功");
        }else {
            return R.fail("失败");
        }
    }

    @RequestMapping("deleteTypes")
    public R deleteTypes(int[] ids){
        int i = articleTypeService.dels(ids);
        if(i > 0){
            return R.ok("批量删除成功");
        }else {
            return R.fail("失败");
        }
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable("id") int id){
        ArticleType articleType = articleTypeService.findById(id);
        return R.ok(articleType);
    }

    @RequestMapping("findById")
    public R findById1(int id){
        ArticleType articleType = articleTypeService.findById(id);
        return R.ok(articleType);
    }

    @RequestMapping("selectAll")
    public R selectAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "2")int pageSize,ArticleType articleType){
        PageInfo<ArticleType> articleTypePageInfo = articleTypeService.selectAll(pageNum, pageSize,articleType);
        return R.ok(articleTypePageInfo);
    }

    @GetMapping("selectAllReally")
    public R selectAll1(){
        List<ArticleType> articleTypes = articleTypeService.selectAll();
        return R.ok(articleTypes);
    }
}
